<?php

class Model_Conference
{
    protected $_table;

    public function getTable()
    {
        if (null === $this->_table) {            
            require_once APPLICATION_PATH . '/models/DbTable/Conference.php';
            $this->_table = new Model_DbTable_Conference;
        }
        return $this->_table;
    }
   
    public function save(array $data)
    {
    		if(isset($data['id'])){
    			$id = $data['id'];
    		}else{
    			$id = 0;
    		}
    		
        $table  = $this->getTable();
        $fields = $table->info(Zend_Db_Table_Abstract::COLS);        
        foreach ($data as $field => $value) {
            if (!in_array($field, $fields)) {
                unset($data[$field]);                
            }
            
        }
        
        if($id == 0){
        	return $table->insert($data);
        }else{
        	$where = $table->getAdapter()->quoteInto('id = ?', $id);
        	return $table->update($data, $where);
        }
        
    }    
	 public function deleteConference($id){
    	$table = $this->getTable();
    	$where = 'id = '.$id;
    	return $table->delete($where); 
    }
    
    public function fetchEntries()
    {       
        return $this->getTable()->fetchAll('1')->toArray();
    }
    public function setActive($id){
        $active = $this->getActive();
        $c = $this->fetchEntry($id);
        if(isset($active) && $c->id == $active->id){
            return;
        }        
        if(isset($active)){
            $active->isActive = 0;
            $active->save();
        }
        $c->isActive = 1;
        $c->save();
    }
    public function fetchEntry($id)
    {
        $table = $this->getTable();
        $select = $table->select()->where('id = ?', $id);    
        return $table->fetchRow($select);
    }
    
    public function getActive(){
        $table = $this->getTable();
        $select = $table->select()->where('isActive = 1');    
        return $table->fetchRow($select);
    }
}